var Algorithm = Class.create({
	initialize : function(outer) {
		this.getRoot.apply(outer, []);
	},
	getRoot : function() {
		var self = this;
		// Issue 29190
		chrome.bookmarks.getChildren('1', function(results) {
					results.each(function(root) {
								if (root.title == 'delicious') {
									self.rootId = root.id;
								}
							});
					if (!self.rootId) {
						chrome.bookmarks.create({
									parentId : '1',
									title : 'delicious'
								}, function(newRoot) {
									if (newRoot)
										self.rootId = newRoot.id;
								});
					}
				});
	},
	loadCache : function() {

	},
	load : function() {

	},
	internalAddBookmark : function(bookmark) {
		chrome.bookmarks.create({
					parentId : this.rootId,
					title : bookmark.name,
					url : bookmark.url
				});
	},

	internalUpdateBookmark : function(bookmark) {
		chrome.bookmarks.getChildren(this.rootId, function(results) {
					results.each(function(result) {
								var toDelete = [];
								results.each(function(result) {
											if (result.url == bookmark.url) {
												// if (bookmark.name !=
												// result.title)
												toDelete.push(result);
											}
										})
								window.setTimeout(function() {
											toDelete.each(function(result) {
												console.log(bookmark.name);
												chrome.bookmarks
														.remove(result.id);
												chrome.bookmarks.create({
															parentId : this.rootId,
															title : bookmark.name,
															url : bookmark.url
														});
													// crashes
													// chrome.bookmarks.update(
													// result.id.toString(), {
													// title : bookmark.name
													// }, function(r) {
													// console.dir(r);
													// });
											})
										}, 0);
							})
				});
	},

	internalRemoveBookmark : function(bookmark) {
		var toDelete = [];
		chrome.bookmarks.getChildren(this.rootId, function(results) {
					results.each(function(result) {
								if (result.url == bookmark.url) {
									toDelete.push(result);
								}
							});

				});
		window.setTimeout(function() {
					toDelete.each(function(result) {
								chrome.bookmarks.remove(result.id);
							})
				}, 1000);
	},

	internalChangeUrl : function(bookmark) {
		// will not happen
		// chrome.bookmarks.getChildren(this.rootId, function(results) {
		// var toDelete = [];
		// results.each(function(result) {
		// if (result.url == bookmark.url) {
		// toDelete.push(result);
		// }
		// })
		// window.setTimeout(function() {
		// toDelete.each(function(result) {
		// chrome.bookmarks.remove(result.id);
		// })
		// chrome.bookmarks.create({
		// parentId : this.rootId,
		// title : bookmark.name,
		// url : bookmark.url
		// });
		// }, 0);
		// });
	},

	internalChangeTag : function(args) {
		// do nothing
	},

	clearRoot : function(callback) {
		var self = this;
		chrome.bookmarks.getChildren(self.rootId, function(results) {
					results.each(function(result) {
								chrome.bookmarks.removeTree(result.id);
							});
					callback.apply(self, []);
				});
	}
});